package com.reliable.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.dto.role.ReliableRoleDTO;
import com.reliable.api.param.IdParam;
import com.reliable.api.param.role.ReliableRoleAddParam;
import com.reliable.api.param.role.ReliableRoleEditParam;
import com.reliable.api.param.role.ReliableRoleParam;

import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.common.utils.StringUtils;
import com.reliable.service.domain.ReliableRole;
import com.reliable.service.service.IReliableRoleService;
import com.reliable.service.utils.IdEncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色Controller
 *
 * @author ruoyi
 * @date 2025-11-27
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/reliable/role")
public class ReliableRoleController  {

    @Autowired
    private IReliableRoleService reliableRoleService;

    /**
     * 查询角色列表
     */
    @ApiOperation("查询角色列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ReliableRoleParam param) {
        Page<ReliableRole> page = new Page<>(param.getPageNum(), param.getPageSize());
        LambdaQueryWrapper<ReliableRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getRoleName() != null && !param.getRoleName().isEmpty(), ReliableRole::getRoleName, param.getRoleName());
        IPage<ReliableRole> pageResult = reliableRoleService.page(page, queryWrapper);
        
        // 转换为DTO
        List<ReliableRoleDTO> dtoList = pageResult.getRecords().stream().map(role -> {
            ReliableRoleDTO dto = new ReliableRoleDTO();
            BeanUtils.copyProperties(role, dto);
            dto.setRoleBusinessId(IdEncryptUtils.encrypt(role.getId()));
            return dto;
        }).collect(Collectors.toList());
        
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setCode(200);
        dataTable.setMsg("查询成功");
        dataTable.setRows(dtoList);
        dataTable.setTotal(pageResult.getTotal());
        return AjaxResult.success(dataTable);
    }

    /**
     * 获取角色详细信息
     */
    @ApiOperation("获取角色详细信息")
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody IdParam param) {
        if (param.getBusinessId() == null) {
            return AjaxResult.error("ID不能为空");
        }
        Long decryptId = IdEncryptUtils.decrypt(param.getBusinessId());
        ReliableRole role = reliableRoleService.getById(decryptId);
        ReliableRoleDTO dto = new ReliableRoleDTO();
        BeanUtils.copyProperties(role, dto);
        dto.setRoleBusinessId(IdEncryptUtils.encrypt(role.getId()));
        return AjaxResult.success(dto);
    }

    /**
     * 新增角色
     */
    @ApiOperation("新增角色")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ReliableRoleAddParam param) {
        ReliableRole reliableRole = new ReliableRole();
        reliableRole.setRoleName(param.getRoleName());
        reliableRole.setDescription(param.getDescription());
        return AjaxResult.success(reliableRoleService.save(reliableRole));
    }

    /**
     * 修改角色
     */
    @ApiOperation("修改角色")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ReliableRoleEditParam param) {
        ReliableRole reliableRole = new ReliableRole();
        reliableRole.setId(IdEncryptUtils.decrypt(param.getRoleBusinessId()));
        reliableRole.setRoleName(param.getRoleName());
        reliableRole.setDescription(param.getDescription());
        return AjaxResult.success(reliableRoleService.updateById(reliableRole));
    }

    /**
     * 删除角色
     */
    @ApiOperation("删除角色")
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody IdParam param) {
        if (param.getBusinessId() == null) {
            return AjaxResult.error("ID不能为空");
        }
        Long decryptId = IdEncryptUtils.decrypt(param.getBusinessId());
        return AjaxResult.success(reliableRoleService.removeById(decryptId));
    }
}